#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <vector>
#include "RenderWindow.h"
#include "Player.h"
#include "Ground.h"
const int WIN_WIDTH = 2048;
const int WIN_HEIGHT = 1280;
const int FRAMERATE = 30;    // 帧频
const int FONT_SIZE = 64;    // 字体大小
SDL_Color black = {0, 0, 0}; // 字体颜色
TTF_Font *font;
RenderWindow renderwindow;
std::vector<SDL_Texture *> playerTex; // 储存人物的动画帧
SDL_Texture *groundTex[4];            // 储存地面瓦片
// 图片加载
void loadPicture();
// 图片销毁
void destroyPicture();

// SDL初始化工作
bool init()
{
    // 初始化SDL
    if (SDL_Init(SDL_INIT_VIDEO))
    {
        SDL_Log("SDL not init，%s", SDL_GetError());
        return false;
    }
    // 初始化字体
    if (TTF_Init())
    {
        SDL_Log("can not init ttf,%s", TTF_GetError());
        return false;
    }
    font = TTF_OpenFont("./res/fonts/Chilanka-Regular.otf", FONT_SIZE);
    if (font == NULL)
    {
        SDL_Log("can not open font");
        return false;
    }
    renderwindow.create("Game Start", WIN_WIDTH, WIN_HEIGHT);

    loadPicture();

    return true;
}

bool load = init();

Player player(0, 0, playerTex.at(0), 0, 47);
Ground ground(groundTex[0], groundTex[1], groundTex[2], groundTex[3]);
void event_loop()
{
    while (1)
    {
        int64_t begin = SDL_GetTicks(); // 获取毫秒值

        renderwindow.clear();

        if (player.isJumping) // 如果是跳跃则跳跃帧播放
        {

            player.currentFrame++;
            if (player.animationFrames == player.currentFrame)
            {
                player.isJumping = false;
                player.currentFrame = 0;
            }
        }
        player.setTex(playerTex.at(player.currentFrame));
        player.update(ground);
        ground.update(player.getScore());

        for (int i = 0; i < ground.getLength(); i++)
        {
            renderwindow.render(ground.getTile(i));
        }
        renderwindow.render(player);
        // 渲染文字
        renderwindow.render(0, 0, player.getScoreString().c_str(), font, black);
        //判断玩家死亡
        if((player.getX()+player.getWidth()<0)|(player.getY()+player.getHight()>WIN_HEIGHT-100))
        {
            player.reset();
            ground.reset();
            renderwindow.render(WIN_WIDTH/2-32, WIN_HEIGHT/2, "Death", font, black);
            renderwindow.display();
            SDL_Delay(2000);
        }
        renderwindow.display();
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                return;
                break;
            case SDL_MOUSEBUTTONDOWN:
                player.handleMouseButtonDown();
                break;
            case SDL_MOUSEBUTTONUP:
                player.handleMouseButtonUp();
                break;
            }
        }

        int64_t current = SDL_GetTicks(); // 获取毫player.秒值
        int64_t cost = current - begin;   // 中间程序所花时间
        int64_t frame = 1000 / FRAMERATE; // 每一帧时间
        int64_t delay = frame - cost;     // 计算中间程序后还剩多少时间
        if (delay > 0)
        {
            SDL_Delay(delay);
        }
    }
}
int main()
{
    //初始化函数写这里 跑外面去是什么意思
    //init()

    event_loop();

    destroyPicture();
    return 0;
}
void loadPicture()
{
    //用一个循环做重复的工作 不累吗 写这么多
    // 加载图片
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player00.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player01.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player02.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player03.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player04.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player05.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player06.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player07.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player08.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player09.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player10.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player11.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player12.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player13.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player14.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player15.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player16.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player17.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player18.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player19.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player20.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player21.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player22.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player23.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player24.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player25.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player26.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player27.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player28.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player29.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player30.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player31.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player32.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player33.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player34.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player35.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player36.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player37.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player38.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player39.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player40.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player41.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player42.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player43.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player44.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player45.png"));
    playerTex.push_back(renderwindow.loadTexture("./res/textures/player/player46.png"));

    groundTex[0] = (renderwindow.loadTexture("./res/textures/ground/left.png"));
    groundTex[1] = (renderwindow.loadTexture("./res/textures/ground/center.png"));
    groundTex[2] = (renderwindow.loadTexture("./res/textures/ground/right.png"));
    groundTex[3] = (renderwindow.loadTexture("./res/textures/ground/hole.png"));
}
void destroyPicture()
{
    for (SDL_Texture *&tex : playerTex)
    {
        SDL_DestroyTexture(tex);
    }
    for (int i = 0; i < 4; i++)
    {
        SDL_DestroyTexture(groundTex[i]);
    }
}